import { SSE } from "sse.js";
import * as R from "ramda";

const connect = R.curryN(6, _connect);

function _connect(url, headers, message, method, onMessage, onError) {
  let sse;
  console.log("connect method", method);
  sse = new SSE(url, { headers, payload: message });
  sse.addEventListener("message", onMessage);
  sse.addEventListener("error", onError);

  return sse;
}

function createSSE(onMessage = R.identity, onError = R.identity) {
  return connect(R.__, R.__, R.__, R.__, onMessage, onError);
}
function sendSSE(sse) {
  sse.stream();
}

export { createSSE, sendSSE };
